﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FormatTest
{
    public abstract class base_func
    {
        public base_func()
        {

        }


        protected string bytes = null;

        public base_func(int index)
        {
            this.index = index;
        }

        /// <summary>
        /// 将二进制无符号字符串转化为十进制
        /// </summary>
        /// <returns></returns>
        protected int TwoToTen(string value)
        {
            return Convert.ToInt32(Common.TwoToTen(value));
        }

        protected int index;

        protected string read_bits(int n)
        {
            return Common.read_bits(bytes, n, ref index);
        }

        protected int read_one_bit()
        {
            return Convert.ToInt32(read_bits(1));
        }

        protected int columbus_ue()
        {
            return ColumbusDecode.Columbus_ue(bytes, ref index);
        }

        protected int columbus_se()
        {
            return ColumbusDecode.Columbus_se(bytes, ref index);
        }

        /// <summary>
        /// 若在比特流中的下一比特为字节的第一个比特，返回true，否则返回false
        /// </summary>
        /// <returns></returns>
        protected bool byte_aligned()
        {
            if (index % 8 == 7)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否为尾比特语法
        /// </summary>
        /// <returns></returns>
        protected virtual void rbsp_trailing_bits()
        {
            try
            {
                var rbsp_stop_one_bit = Convert.ToInt32(read_bits(1));
                while (!byte_aligned())
                {
                    var rbsp_alignment_zero_bit = Convert.ToInt32(read_bits(1));
                }
            }
            catch { }
        }

        /// <summary>
        /// 判断是否还有多余的rbsp比特串
        /// </summary>
        /// <returns></returns>
        protected bool more_rbsp_data()
        {
            try
            {
                var rbsp_stop_one_bit = Convert.ToInt32(read_bits(1));
                if (rbsp_stop_one_bit != 1)
                {
                    return true;
                }
                while (!byte_aligned())
                {
                    var rbsp_alignment_zero_bit = Convert.ToInt32(read_bits(1));
                    if (rbsp_alignment_zero_bit != 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return true;
            }
        }

    }
}
